package com.lovo.ssc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.ssc.bean.Order;
import com.lovo.ssc.bean.OrderGoods;
import com.lovo.ssc.service.IOrderGoodsService;
import com.lovo.ssc.service.IOrderService;
import com.lovo.ssc.vo.OrderInfoVo;
import com.lovo.sscweb.dto.OrderDto;
import com.lovo.sscweb.dto.OrderItemTo;
import com.lovo.util.Verifier;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-11
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单控制器---王孝亮")
public class OrderController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    IOrderGoodsService orderGoodsService;

    @GetMapping("confirmOrder")
    @ApiOperation("确认订单")
    public OrderInfoVo confirmOrder() {
        return orderService.confirmOrder();
    }

    @GetMapping("submitOrder/{addressId}/{payMethod}")
    @ApiOperation("提交订单")
    public OrderDto submitOrder(@PathVariable int addressId, @PathVariable int payMethod) {
        return orderService.submitOrder(addressId, payMethod);
    }

    @PostMapping("directBuy")
    @ApiOperation("直接购买")
    public OrderInfoVo directBuy(OrderItemTo commodity) {
        return orderService.directBuy(commodity);
    }

    @PostMapping("directSubmit/{addressId}/{payMethod}")
    @ApiOperation("直接购买确认")
    public OrderDto directSubmit(@PathVariable int addressId, @PathVariable int payMethod,OrderItemTo commodity) {
        return orderService.directSubmit(addressId, payMethod,commodity);
    }

    //    @ApiOperation("消费：用户code，消费金额，消费明细")
    //    @GetMapping("consume/{userCode}/{consumeMoney}/{goodsInfo}")
    //    public boolean consumption(@PathVariable("userCode") String userCode,
    //                               @PathVariable("consumeMoney") float consumeMoney,
    //                               @PathVariable("goodsInfo") String goodsInfo) {
    //        return orderService.consumption(userCode, consumeMoney, goodsInfo);
    //    }

    @GetMapping("consumption/{addressId}/{payMethod}")
    @ApiOperation("第三方支付")
    public boolean consumption(@PathVariable int addressId,@PathVariable int payMethod){
        return orderService.consumption(addressId,payMethod);
    }

    @ApiOperation("获取支付结果-tag=0 正常 1-失败 2-超支")
    @GetMapping("consumeConfirm/{userCode}")
    public Object consumeConfirm(@PathVariable("userCode") String userCode) {
        return orderService.getPayResult(userCode);
    }

    @PostMapping("directThird/{addressId}/{payMethod}")
    public boolean directThird(@PathVariable int addressId, @PathVariable int payMethod,OrderItemTo commodity){
        return orderService.directThird(addressId, payMethod, commodity);
    }

    @GetMapping("findOrderState/{orderCode}")
    @ApiOperation("按订单编号查询订单状态")
    public int findOrderState(@PathVariable String orderCode) {
        return orderService.findOrderState(orderCode);
    }

    @ApiOperation("按订单编号修改状态")
    @PutMapping("updateOrderState/{orderCode}")
    public void updateOrderState(@PathVariable String orderCode) {
        orderService.updateOrderState(orderCode);
    }

    @GetMapping("findByTime")
    @ApiOperation(value = "查询所有订单以及，按时间查询订单")
    public List<Order> findByTime(String startTime, String endTime, HttpServletRequest request) {
        String token = request.getHeader("token");
        String userName = Verifier.verifierJwt(token);


        return orderService.findByItem(userName, startTime, endTime);
    }

    @DeleteMapping("delByOrderId/{orderId}")
    @ApiOperation("根据id删除订单")
    public String delByOrderId(@PathVariable("orderId") int orderId) {
        System.out.println(orderId);
        Order order = orderService.getById(orderId);
        QueryWrapper<OrderGoods> qw = new QueryWrapper<>();
        qw.eq("orderCode", order.getOrderCode());
        orderGoodsService.remove(qw);

        orderService.removeById(orderId);
        return "ok";
    }


}
